import type {
  AddReq,
  DelReq,
  EditReq,
  InfoReq,
  UserPageRes,
} from '@fast-crud/fast-crud';

import type { AxiosRequestConfig } from '@vben/request';

import type { DefDatasourceConfigModel, PageParams, PageResult } from '#/api';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';
import { ContentTypeEnum } from '#/enums/httpEnum';

const MODULAR = 'defDatasourceConfig';

export const DefDatasourceConfigConfig = {
  Save: {
    url: `${ServicePrefixEnum.SYSTEM}/${MODULAR}`,
    method: 'POST',
  } as AxiosRequestConfig,
  Update: {
    url: `${ServicePrefixEnum.SYSTEM}/${MODULAR}`,
    method: 'PUT',
  } as AxiosRequestConfig,
};

export namespace DefDatasourceConfigApi {
  /**
   * 分页查询
   */
  export async function page(
    params: PageParams<DefDatasourceConfigModel.DefDatasourceConfigPageQuery>,
  ) {
    return requestClient.post<
      PageResult<DefDatasourceConfigModel.DefDatasourceConfigResultVO>
    >(`${ServicePrefixEnum.SYSTEM}/${MODULAR}/page`, params);
  }

  export async function query(
    params: DefDatasourceConfigModel.DefDatasourceConfigPageQuery,
  ) {
    return requestClient.post<
      DefDatasourceConfigModel.DefDatasourceConfigResultVO[]
    >(`${ServicePrefixEnum.SYSTEM}/${MODULAR}/query`, params);
  }

  /**
   * 保存
   */
  export async function save(
    params: DefDatasourceConfigModel.DefDatasourceConfigSaveVO,
  ) {
    return requestClient.post<DefDatasourceConfigModel.DefDatasourceConfigResultVO>(
      DefDatasourceConfigConfig.Save.url as string,
      params,
    );
  }

  /**
   * 修改
   */
  export async function update(
    params: DefDatasourceConfigModel.DefDatasourceConfigUpdateVO,
  ) {
    return requestClient.put<DefDatasourceConfigModel.DefDatasourceConfigResultVO>(
      DefDatasourceConfigConfig.Update.url as string,
      params,
    );
  }

  /**
   * 删除
   */
  export async function remove(data: string[]) {
    return requestClient.delete<boolean>(
      `${ServicePrefixEnum.SYSTEM}/${MODULAR}`,
      { data },
    );
  }

  /**
   * 测试链接
   */
  export async function testConnect(id: string) {
    return requestClient.post<DefDatasourceConfigModel.DefDatasourceConfigResultVO>(
      `${ServicePrefixEnum.SYSTEM}/${MODULAR}/testConnect`,
      { id },
      { headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED } },
    );
  }

  // fast-crud 需要的接口
  export const pageRequest = async (
    pageQuery: PageParams<DefDatasourceConfigModel.DefDatasourceConfigPageQuery>,
  ): Promise<UserPageRes> => {
    return await page(pageQuery);
  };

  export const infoRequest = async (ctx: InfoReq): Promise<UserPageRes> => {
    const { row } = ctx;
    // 请求后台查询数据
    return row;
  };

  export const addRequest = async (req: AddReq) => {
    const { form } = req;
    return await save(form);
  };

  export const editRequest = async (ctx: EditReq) => {
    const { form } = ctx;
    return await update(form);
  };

  export const delRequest = async (ctx: DelReq) => {
    const { row } = ctx;
    return await remove([row.id]);
  };
}
